package com.hp.tiger.chapter13;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author XavierDong
 **/
public class ConsumerThread extends Thread {

    private final MessageQueue messageQueue;

    private static final Random RANDOM = new Random(System.currentTimeMillis());

    public ConsumerThread(MessageQueue messageQueue, int seq) {
        super("CONSUMER - " + seq);
        this.messageQueue = messageQueue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Message take = messageQueue.take();
                System.out.println(Thread.currentThread().getName() + " take message " + take.getData());
                TimeUnit.MILLISECONDS.sleep(RANDOM.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
